package com.example.mybatis.controller;

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.example.mybatis.aa.mapper.UserMapper;
import com.example.mybatis.aa.model.User;
import com.example.mybatis.model.ResponseModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * controller
 *
 * @author loutong
 * @date 2021-06-04 10:29:12
 */
@RestController
public class MyController {


    @NacosValue(value = "${useLocalCache:false}", autoRefreshed = true)
    private String useLocalCache;

    @Autowired
    UserMapper userMapper;

    @NacosInjected
    private NamingService namingService;

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public ResponseModel<Object> get() {
        ResponseModel<Object> objectResponseModel = new ResponseModel<>();
        objectResponseModel.setDate(useLocalCache);
        return objectResponseModel;
    }

    @RequestMapping(value = "/get1", method = RequestMethod.GET)
    public List<Instance> get1(@RequestParam String serviceName) throws NacosException {
        return namingService.getAllInstances(serviceName);
    }

    @GetMapping(value = "/getUser")
    public ResponseModel<List<User>> getUser() {
        List<User> users = userMapper.selectAll();
        return new ResponseModel<>(0, "OK", users);
    }

}
